export const intersection = <T>(arr1: T[], arr2: T[]) => {
  const arr = new Set(arr1);
  return arr2.filter((item: T) => arr.has(item));
};
export const flatten = <T>(arr: T[][], depth = 1): T[] =>
  arr.reduce(
    (a, v) =>
      a.concat(
        depth > 1 && Array.isArray(v) ? flatten(v as any[], depth - 1) : v
      ),
    []
  );
export const unique = <T>(arr: T[]) => [...new Set(arr)];
export const union = <T>(...args: T[][]) => unique(flatten(args, 1));
export const difference = <T>(arr1: T[], arr2: T[]) => {
  const allList = union(arr1, arr2);
  const intersectionList = intersection(arr1, arr2);
  return allList.filter((item: T) => !intersectionList.includes(item));
};
